// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SettingsGroupsCheckedListBox.cs" company="Roman '4ux-nbIx' Novitsky">
//   Copyright (c) Roman '4ux-nbIx' Novitsky 2009
// </copyright>
// <summary>
//   Defines the SettingsGroupsCheckedListBox type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace SettingsManager
{
  #region Namespace Imports

  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Linq;
  using System.Windows.Forms;

  using ComponentSettingsManagers;

  using JetBrains.Annotations;

  #endregion


  /// <summary>
  /// Describes and manipulates the settings groups checked list box.
  /// </summary>
  [UsedImplicitly(ImplicitUseKindFlags.Default, ImplicitUseTargetFlags.WithMembers)]
  public sealed class SettingsGroupsCheckedListBox : CheckedListBox
  {
    #region Constructors and Destructors

    /// <summary>
    /// Initializes a new instance of the <see cref="SettingsGroupsCheckedListBox"/> class.
    /// </summary>
    public SettingsGroupsCheckedListBox()
    {
      try
      {
        foreach (
          IComponentSettingsManager manager in
            ResharperSettingsManager.Instance.ComponentSettingsManagers.OrderBy(m => m.Name))
        {
          Items.Add(manager, true);
        }
      }
      catch
      {
        return;
      }
    }

    #endregion


    #region Properties

    /// <summary>
    /// Gets the selected settings manager.
    /// </summary>
    internal IComponentSettingsManager SelectedSettingsManager
    {
      get
      {
        return (IComponentSettingsManager)SelectedItem;
      }
    }

    /// <summary>
    /// Gets or sets the selected settings managers.
    /// </summary>
    /// <value>
    /// The selected settings managers.
    /// </value>
    [NotNull]
    [EditorBrowsable(EditorBrowsableState.Never)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    [Browsable(false)]
    internal IEnumerable<IComponentSettingsManager> SelectedSettingsManagers
    {
      get
      {
        return CheckedItems.Cast<IComponentSettingsManager>().ToList();
      }

      set
      {
        Items.Clear();

        foreach (
          IComponentSettingsManager manager in
            ResharperSettingsManager.Instance.ComponentSettingsManagers.OrderBy(m => m.Name))
        {
          Items.Add(manager, value.Contains(manager));
        }
      }
    }

    #endregion
  }
}